package com.maysham.coder.server.controller;

import com.maysham.coder.server.common.Response;
import com.maysham.coder.server.model.Code;
import com.maysham.coder.server.request.CodeCreateReq;
import com.maysham.coder.server.request.CodePageReq;
import com.maysham.coder.server.request.CodeUpdateReq;
import com.maysham.coder.server.service.CodeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/api/code", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CodeController {
    @Resource
    private CodeService codeService;

    @GetMapping("/page")
    public Response<Page<Code>> codePage(@RequestBody @Valid CodePageReq req) {
        return Response.ok(codeService.codePage(req.getGroupId(), PageRequest.of(req.getPage(), req.getSize())));
    }

    @PostMapping("/create")
    public Response<Code> create(@RequestBody @Valid CodeCreateReq req){
        return Response.ok(codeService.create(req));
    }

    @PostMapping("/update")
    public Response<Code> update(@RequestBody @Valid CodeUpdateReq req){
        return Response.ok(codeService.update(req));
    }
}
